<template>
	<view class="content">
		<view class="form">
			<up-form :model="formData">
				<up-form-item prop="content">
					<up-textarea v-model="formData.content" placeholder="请输入内容" count></up-textarea>
				</up-form-item>


				<up-form-item prop="picurls">
					<up-upload :fileList="formData.imageValue" capture @afterRead="afterRead" @delete="deletePic" name="4"
						multiple :maxCount="9"></up-upload>
				</up-form-item>



			</up-form>
			<up-button shape="circle" :disabled="formData.imageValue.length	== 0" type="success" @click="submitForm">提交</up-button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		watch
	} from 'vue';
	import {
		onLoad
	} from "@dcloudio/uni-app"
	const db = uniCloud.database()
	let types = null
	const formData = ref({
		content: '',
		imageValue: []
	})
	// 上传图片
	const afterRead = async (event) => {
		let lists = [].concat(event.file);
		let fileListLen = formData.value.imageValue.length;
		lists.map((item) => {
			formData.value.imageValue.push({
				...item,
				status: 'uploading',
				message: '上传中',
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const result = await uploadFilePromise(lists[i].url, lists[i].name);
			let item = formData.value.imageValue[fileListLen];
			formData.value.imageValue.splice(fileListLen, 1, {
				...item,
				status: 'success',
				message: '',
				url: result,
			});
			fileListLen++;
		}
	}
	const uploadFilePromise = (url, name = ".jpg") => {
		return new Promise((resolve, reject) => {
			uniCloud.uploadFile({
				url: 'https://file-univknpors-mp-cdb1ede8-1736-48cd-b071-5098a2bf31e1.oss-cn-zhangjiakou.aliyuncs.com', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				cloudPath: Date.now() + '-' + name,
				formData: {
					name: url.name
				},
				success: (res) => {
					resolve(res.fileID)
				},
				fail: (err) => {
					reject(err);
				}
			});
		})
	}
	// 删除图片
	const deletePic = (event) => {
		formData.value.imageValue.splice(event.index, 1);
	}

	const submitForm = async (e) => {
		setTimeout(()=>{
		},1000)
		// 修改文章
		if (types == 1) {
			let id = formData.value._id
			db.collection('articles').doc(id).update({
					content: formData.value.content,
					imageValue: formData.value.imageValue
				})
				.then(res => {
					uni.showToast({
						title: '修改成功',
						"icon": 'success'
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}, 800)
				}).catch(err=>{
					uni.showToast({
						title:err.message,
						icon:"error"
					})
				})
			return
		}
		// 添加文章
		db.collection('articles').add({
			content: formData.value.content,
			imageValue: formData.value.imageValue,
			posttime: Date.now()
		}).then(res => {
			uni.showToast({
				title: '发布成功',
				"icon": 'success'
			})
			setTimeout(() => {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 800)
		}).catch(err => {
			uni.showToast({
				title: err.message,
				icon: "none"
			})
		})
	}

	onLoad(async (e) => {
		const {
			id,
			type
		} = e
		types = type
		// 获取单条文章
		if (type) {
			db.collection('articles').doc(id).get()
				.then(res => {
					formData.value = res.result.data[0]
				})
		}
	})
</script>

<style lang="scss">
</style>